//
//  AlertWindow.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/5.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "AlertWindow.h"

@interface AlertWindow()
@property (nonatomic, strong) UIView *BGView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIButton *submitBtn;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, assign) CGFloat sHeight;
@property (nonatomic, assign) CGFloat sWidth;

#define  scaleHeight  [UIScreen mainScreen].bounds.size.height/568
#define  scaleWidth  [UIScreen mainScreen].bounds.size.width/320


@end


@implementation AlertWindow
+ (AlertWindow *)sharedInstance{
    static id sharedInstance = nil;
    static dispatch_once_t onceTocken;
    
    dispatch_once(&onceTocken, ^{
        sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    });
    
    return sharedInstance;
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.sHeight = scaleHeight;
        if (self.sHeight < 1.0) {
            self.sHeight = 1.0;
        }
        self.sWidth=scaleWidth;
        
        
        self.windowLevel = UIWindowLevelAlert;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self creatView];
    }
    return self;
}

- (void)creatView{
    [self bk_whenTapped:^{
        self.hidden =YES;
    }];
    CGFloat width=WindowWidth/3*2;
    self.BGView = [[UIView alloc] initWithFrame:CGRectMake((WindowWidth-width)/2, 0, width, 110)];
    self.BGView.backgroundColor = [UIColor whiteColor];
    self.BGView.layer.cornerRadius = 5 ;
    self.BGView.layer.masksToBounds = YES;
    CGPoint point = self.center;
    self.BGView.center = point;
    [self addSubview:self.BGView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 , 10 , CGRectGetWidth(self.BGView.bounds) - 20, 15)];
    [self.BGView addSubview:self.titleLabel];
    
    [self.titleLabel setTextColor:[UIColor blackColor]];
    self.titleLabel.font = F(13);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.textLabel = [[UILabel alloc] init];
    [self.BGView addSubview:self.textLabel];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel).offset(20);
        make.left.equalTo(self.BGView).offset(10);
        make.right.equalTo(self.BGView).offset(-10);
    }];
    self.textLabel.numberOfLines =0;
    self.textLabel.font = F(12);
    self.textLabel.textColor =[UIColor colorFromHexString:@"4a4949"];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    
    self.submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.submitBtn.tag = 0;
    CGFloat btnWidth=(width-40-10)/2;
    self.submitBtn.frame = CGRectMake(20,self.BGView.bounds.size.height-45,btnWidth, 25);
    [self.submitBtn setTitle:self.oneBtnTitle forState:UIControlStateNormal];
    [self.submitBtn setBackgroundColor:MainColor];
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.submitBtn.layer.masksToBounds =YES;
    self.submitBtn.layer.cornerRadius =5;
    
    self.submitBtn.titleLabel.font = F(12);
    [self.submitBtn addTarget:self action:@selector(submitBtnPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.BGView addSubview:self.submitBtn];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.tag = 1;
    self.cancelBtn.frame = CGRectMake(CGRectGetMaxX(self.submitBtn.frame)+10, CGRectGetHeight(self.BGView.bounds) - 45 , btnWidth, 25);
    [self.cancelBtn setTitle:self.twoBtnTitle forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:MainColor forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = F(12);
    self.cancelBtn.layer.borderColor =MainColor.CGColor;
    self.cancelBtn.layer.borderWidth =0.5;
    [self.cancelBtn addTarget:self action:@selector(submitBtnPress:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.hidden = YES;
    
    [self.BGView addSubview:self.cancelBtn];
    
    
}

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}

- (void)setContext:(NSString *)text{
    self.textLabel.text = text;
}


- (void)show{
    [self makeKeyWindow];
    self.hidden = NO;
}

- (void)submitBtnPress:(UIButton *)button{
    [self resignKeyWindow];
    self.hidden = YES;
    if (button.tag == 0) {
        if (self.afterOneBtn) {
            self.afterOneBtn();
        }
    }else if (button.tag==1)
    {
        if (self.afterTwoBtn) {
            self.afterTwoBtn();
        }
    }
    
}


- (void)setShowCancleBtn:(BOOL)showCancleBtn{
    _showCancleBtn = showCancleBtn;
    if (_showCancleBtn) {
        self.cancelBtn.hidden = NO;
        CGFloat width=WindowWidth/3*2;
        CGFloat btnWidth=(width-40-10)/2;
        self.submitBtn.frame = CGRectMake(20,self.BGView.bounds.size.height-45,btnWidth, 25);
        [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.submitBtn.layer.masksToBounds =YES;
        self.submitBtn.layer.cornerRadius =5;
        [self.submitBtn setTitle:self.oneBtnTitle forState:UIControlStateNormal];
        
        self.cancelBtn.frame = CGRectMake(CGRectGetMaxX(self.submitBtn.frame)+10, CGRectGetHeight(self.BGView.bounds) - 45 , btnWidth, 25);
        [self.cancelBtn setTitleColor:MainColor forState:UIControlStateNormal];
        self.cancelBtn.layer.masksToBounds =YES;
        self.cancelBtn.layer.cornerRadius =5;
        [self.cancelBtn setTitle:self.twoBtnTitle forState:UIControlStateNormal];
        self.cancelBtn.layer.borderColor =MainColor.CGColor;
        self.cancelBtn.layer.borderWidth =0.5;
    }
    else{
        self.cancelBtn.hidden = YES;
        self.submitBtn.frame = CGRectMake(20, CGRectGetHeight(self.BGView.bounds) - 45, self.BGView.bounds.size.width-40, 25);
        self.submitBtn.layer.masksToBounds =YES;
        self.submitBtn.layer.cornerRadius =5;
        [self.submitBtn setTitle:self.oneBtnTitle forState:UIControlStateNormal];
        self.submitBtn.layer.borderColor =MainColor.CGColor;
        self.submitBtn.layer.borderWidth =0.5;
        
    }
}

@end
